//
//  PlayListHomeViewController.swift
//  GhostWorld
//
//  Created by Kam on 2018/5/14.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit

private let playListTableCellIdentifier = "PlayListTableCell"
private let noDataCellIdentifier = "NoDataTableCell"

class PlayListHomeViewController: BaseViewController {

  // MARK: - IBOutlets
  
  @IBOutlet private weak var tableView: UITableView!
  
  // MARK: - Private vars
  
  private var playlists: [Playlist]?
  private var selectedPlaylist: Playlist?
  
  // MARK: - Private methods
  
  private func prepareUI() {
    tableView.register(UINib(nibName: playListTableCellIdentifier, bundle: nil), forCellReuseIdentifier: playListTableCellIdentifier)
    tableView.register(UINib(nibName: noDataCellIdentifier, bundle: nil), forCellReuseIdentifier: noDataCellIdentifier)
    
    tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 40, right: 0)
    tableView.scrollIndicatorInsets = tableView.contentInset
  }
  
  // MARK: - Public vars
  
  
  
  // MARK: - Lifecycle
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    prepareUI()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    playlists = Playlist.getAll()
    tableView.reloadData()
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowPlaylistDetailSegue" {
      let detailViewController = segue.destination as! PlayListDetailViewController
      detailViewController.playlist = selectedPlaylist
    }
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  // MARK: - IBActions
  
  
  
  // MARK: - Public methods
  
  
  
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension PlayListHomeViewController: UITableViewDataSource {
  func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let count = playlists?.count, count > 0 {
      return count
    } else {
      return 1
    }
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let count = playlists?.count, count > 0 {
      // playlist cell
      let cell = tableView.dequeueReusableCell(withIdentifier: playListTableCellIdentifier, for: indexPath) as! PlayListTableCell
      cell.prepare(playlist: playlists![indexPath.row], canDisplayArrow: true)
      return cell
    } else {
      // no data cell
      let cell = tableView.dequeueReusableCell(withIdentifier: noDataCellIdentifier, for: indexPath) as! NoDataTableCell
      cell.tipLabel.text = "没有听单"
      return cell
    }
  }
}

extension PlayListHomeViewController: UITableViewDelegate {
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let count = playlists?.count, count > 0,
      let playlist = playlists?[indexPath.row] {
      
      selectedPlaylist = playlist
      performSegue(withIdentifier: "ShowPlaylistDetailSegue", sender: nil)
    }
    
    tableView.deselectRow(at: indexPath, animated: true)
  }
  
  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if let count = playlists?.count, count > 0 {
      return true
    } else {
      return false
    }
  }
  
  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
      if let playlist = playlists?[indexPath.row],
        let index = playlists?.index(of: playlist) {
        do {
          try Playlist.remove(playlist: playlist)
          playlists?.remove(at: index)
          if let count = playlists?.count, count > 0 {
            tableView.deleteRows(at: [indexPath], with: .automatic)
          } else {
            tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
          }
        } catch { }
      }
    }
  }
  
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if let count = playlists?.count, count > 0 {
      return PlayListTableCell.defaultHeight
    } else {
      return NoDataTableCell.defaultHeight
    }
  }
}
